package org.jeecg.modules.compilation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.compilation.entity.CiBusinessCategory;
import org.jeecg.modules.compilation.entity.CiBusinessCategoryUser;
import org.jeecg.modules.compilation.mapper.CiBusinessCategoryUserMapper;
import org.jeecg.modules.compilation.service.ICiBusinessCategoryService;
import org.jeecg.modules.compilation.service.ICiBusinessCategoryUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 用户icon关联表
 * @Author: jeecg-boot
 * @Date:   2024-03-15
 * @Version: V1.0
 */
@Service
public class CiBusinessCategoryUserServiceImpl extends ServiceImpl<CiBusinessCategoryUserMapper, CiBusinessCategoryUser> implements ICiBusinessCategoryUserService {

    @Autowired
    private ICiBusinessCategoryService ciBusinessCategoryService;

    /**
     * 获取图标列表
     * @return
     */
    @Override
    public List<CiBusinessCategoryUser> getIconList() {
        //获取登录用户信息
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = loginUser.getId();

        //根据 用户id、enableFlag=1 获取图标列表数据
        LambdaQueryWrapper<CiBusinessCategoryUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CiBusinessCategoryUser::getUserId,userId);
        lambdaQueryWrapper.eq(CiBusinessCategoryUser::getEnableFlag,"1");

        return this.list(lambdaQueryWrapper);
    }

    /**
     * 根据 用户id、enableFlag=0 查询数据
     */
    @Override
    public List<CiBusinessCategory> addIcon() {
        //获取登录用户信息
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = loginUser.getId();

        //获取 ciBusinessCategory表 数据 列表
        List<CiBusinessCategory> ciBusinessCategoryList = ciBusinessCategoryService.list();

        //根据 用户id、enableFlag=1 获取图标列表数据
        LambdaQueryWrapper<CiBusinessCategoryUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CiBusinessCategoryUser::getUserId,userId);
        lambdaQueryWrapper.eq(CiBusinessCategoryUser::getEnableFlag,"1");
        List<CiBusinessCategoryUser> ciBusinessCategoryUserList = this.list(lambdaQueryWrapper);

        //合并两个列表并获取EnableFlag=0的数据
        ciBusinessCategoryList.removeAll(ciBusinessCategoryUserList);

        return ciBusinessCategoryList;
    }

    /**
     * 更新图标状态
     */
    @Override
    public boolean updateIcon(String businessCategorySn) {
        try {
            //获取登录用户信息
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String userId = loginUser.getId();

            //根据 用户id、图标编号 获取数据
            LambdaQueryWrapper<CiBusinessCategoryUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(CiBusinessCategoryUser::getUserId,userId);
            lambdaQueryWrapper.eq(CiBusinessCategoryUser::getBusinessCategorySn,businessCategorySn);
            CiBusinessCategoryUser ciBusinessCategoryUser = this.getOne(lambdaQueryWrapper);

            //更改图标状态
            ciBusinessCategoryUser.setEnableFlag(ciBusinessCategoryUser.getEnableFlag().equals("1")?"0":"1");

            //更新
            this.updateById(ciBusinessCategoryUser);
        } catch (Exception e){
            return false;
        }
        return true;
    }

}
